package cn.wolfcode.wms.web.controller;


import cn.wolfcode.wms.domain.Brand;
import cn.wolfcode.wms.domain.Product;
import cn.wolfcode.wms.query.ProductQueryObject;
import cn.wolfcode.wms.query.QueryObject;
import cn.wolfcode.wms.service.IBrandService;
import cn.wolfcode.wms.service.IProductService;
import cn.wolfcode.wms.util.JSONResult;
import cn.wolfcode.wms.util.NeedPermission;
import cn.wolfcode.wms.util.PageResult;
import cn.wolfcode.wms.util.UploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import sun.rmi.server.InactiveGroupException;

import javax.servlet.ServletContext;

@Controller
@RequestMapping("product")
public class ProductController {
    @Autowired
    private IProductService productService;
    @Autowired
    private IBrandService brandService;
    @Autowired
    private ServletContext sct;//网站的上下文(这个内置对象可以获取到服务器路径)

    @RequestMapping("list")
    public String list(@ModelAttribute("qo") ProductQueryObject qo, Model model){
        PageResult pageResult = productService.queryResult(qo);
        model.addAttribute("result",pageResult);
        model.addAttribute("brands",brandService.selectList());
        return "product/list";
    }
    @RequestMapping("productListView")
    public String productListView(@ModelAttribute("qo") ProductQueryObject qo, Model model){

        qo.setPageSize(20);
        PageResult pageResult = productService.queryResult(qo);
        model.addAttribute("result",pageResult);
        model.addAttribute("brands",brandService.selectList());
        return "product/productListView";
    }

    @RequestMapping("input")
    public String input(Model model,Long id){

        if (id != null) {
            model.addAttribute("entity",productService.get(id));
        }
        model.addAttribute("brands",brandService.selectList());

        return "product/input";
    }

    @RequestMapping("delete")
    @ResponseBody
    public Object delete(Long id,String imagePath){

        //先要判断是否有图片,如果没有图片则不用删除图片,直接执行delete操作
        if (StringUtils.hasLength(imagePath)) {
            UploadUtil.deleteFile(sct,imagePath);
        }

        productService.delete(id);

        return new JSONResult();
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody//需要配置文件上传解析器,要不然执行不了这个方法
    public Object saveOrUpdate(Product d,MultipartFile proImage){

        //处理图片(判断上是否上传有图片过来)
        if (proImage != null && proImage.getContentType().startsWith("image/")) {
            //这里的判断为了防止以前就没有图片
            if (StringUtils.hasLength(d.getImagePath())) {
                UploadUtil.deleteFile(sct,d.getImagePath());
            }
            //获取要保存到服务器上文件夹的路径
            String dir = sct.getRealPath("/upload");
            String filePath = UploadUtil.upload(proImage, dir);
            //把图片路径保存到对象中去
            d.setImagePath(filePath);
        }
        //因为没有使用到第三范式,所有这里需要查询品牌的名称
        Brand brand = brandService.get(d.getBrandId());
        //设置品牌的名称
        d.setBrandName(brand.getName());
        productService.saveOrUpdate(d);
        return new JSONResult();//象征想返回
    }
}
